//
//  oldHistoryOrderListViewController.m
//  laonianbangSupport
//
//  Created by 勇拓 李 on 18/3/6.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldHistoryOrderListViewController.h"
#import "oldTopChooseScrollView.h"
#import "oldHistoryOrderListTableViewCell.h"
#import "oldService.h"
#import "OrderInfoViewController.h"

@interface oldHistoryOrderListViewController ()<TopChooseDelegate>
@property (nonatomic, strong)oldTopChooseScrollView *topScrollView;
@property (nonatomic, copy)NSArray *dataArray;
@property (nonatomic, copy)NSArray *serviceArray;
@property (nonatomic, copy)NSString *cid;
@end

static NSString *const identifier = @"oldHistoryOrderListTableViewCell";

@implementation oldHistoryOrderListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    [self.tableView registerNib:[UINib nibWithNibName:identifier bundle:nil] forCellReuseIdentifier:identifier];
    self.navigationItem.title = @"历史订单";
}

- (void)initTopScrollView:(NSArray *)array
{
    [_topScrollView initWithList:array width:50.f];
}

#pragma mark - lazyLoad
- (oldTopChooseScrollView *)topScrollView
{
    if (!_topScrollView) {
        _topScrollView = [[[NSBundle mainBundle] loadNibNamed:@"oldTopChooseScrollView"
                                                        owner:self
                                                      options:nil] objectAtIndex:0];
        
        _topScrollView.delegate = self;
        
        NSMutableArray *titleArray = [NSMutableArray array];
        [titleArray addObject:@"全部"];
        
        for (int i = 0; i < [_serviceArray count]; i++) {
            
            oldService *model = _serviceArray[i];
            
            [titleArray addObject:model.cname];
        }
        
        [_topScrollView initWithList:titleArray width:80.f];
    }
    
    return _topScrollView;
}

#pragma mark - initData
- (void)initData
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    
    WeakSelf;
    
    [oldHub waiting:@"加载中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_GetAvailableList params:@{ @"pid" : @"0" } success:^(id data) {
        [oldHub hideHUD];
        if (data) {
            [weakSelf initDataSource:data];
            [weakSelf.tableView.mj_header beginRefreshing];
            [weakSelf.tableView reloadData];
        }
        
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (void)refresh
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"pageNum", @"10", @"pageSize", nil];
    if (_cid) {
        [params setObject:_cid forKey:@"cid"];
    }
    
    [self loadData:params ifResetPageNo:YES];
}

- (void)loadMore
{
    NSString *pageNum = [NSString stringWithFormat:@"%ld", (long)pageNo + 1];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:pageNum, @"pageNum", @"10", @"pageSize", nil];
    if (_cid) {
        [params setObject:_cid forKey:@"cid"];
    }
    
    [self loadData:params ifResetPageNo:NO];
}

- (void)loadData:(NSDictionary *)params ifResetPageNo:(BOOL)reset
{
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_GetOrderList params:params success:^(id data) {
        if (reset) {
            pageNo = 1;
            weakSelf.dataArray = [NSArray array];
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            pageNo ++;
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        
        [weakSelf initOrderList:data];
        [weakSelf.tableView reloadData];
        
    } error:^(NSString *error) {
        if (reset) {
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        [oldHub warningUser:error];
    }];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [NSMutableArray array];
    
    for (int i = 0; i < [data count]; i++) {
        oldService *model = [[oldService alloc] initWithDictionary:data[i]];
        [array addObject:model];
    }
    
    _serviceArray = array;
}

- (void)initOrderList:(id)data
{
    NSMutableArray *array = [_dataArray mutableCopy];
    
    for (int i = 0; i < [data count]; i++) {
        oldOrderInfo *orderInfo = [[oldOrderInfo alloc] initWithDictionary:data[i]];
        [array addObject:orderInfo];
    }
    
    _dataArray = array;
}

#pragma mark - topViewDelegate
- (void)topScrollViewChoosed:(NSUInteger)index
{
    NSString *oldCid = _cid;
    
    if (index == 0) {
        _cid = nil;
    }
    else
    {
        oldService *service = _serviceArray[index - 1];
        _cid = service.cid;
    }
    
    if (![oldCid isEqualToString:_cid]) {
        [self.tableView.mj_header beginRefreshing];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (![_serviceArray count]) {
        return 0;
    }
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    oldHistoryOrderListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldHistoryOrderListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.orderInfo = _dataArray[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 135.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    oldOrderInfo *order = _dataArray[indexPath.row];
    if ([order ifCanceled]) {
        return;
    }
    
    OrderInfoViewController *vc = [[OrderInfoViewController alloc] init];
    vc.orderInfo = order;
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 52)];
    
    [view addSubview:self.topScrollView];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 52.f;
}

@end
